package com.qfedu.repo.dao.impl;

import com.qfedu.repo.bean.Manager;
import com.qfedu.repo.dao.ManagerDao;
import com.qfedu.repo.db.DBUtils;
import com.qfedu.repo.db.PstCallback;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ManagerDaoImpl implements ManagerDao {

    /**
     * 后台管理员登录功能
     *
     * @param account 账号
     * @param pwd     密码
     * @return
     */
    @Override
    public Manager login(String account, String pwd) {
        Manager manager = null;
        //1.sql语句
        String sql = "select id,account,password,phone,level,remark from manager where account = ? and password = ?";
        ResultSet result = DBUtils.query(sql, new PstCallback() {
            @Override
            public void pstSetValue(PreparedStatement pst) {
                try {
                    pst.setString(1, account);
                    pst.setString(2, pwd);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        try {
            while (result != null && result.next()) {
                int id = result.getInt("id");
                String user = result.getString("account");
                String userpwd = result.getString("password");
                String phone = result.getString("phone");
                String level = result.getString("level");
                String remark = result.getString("remark");
                manager = new Manager(id, user, userpwd, phone, level, remark);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                DBUtils.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return manager;
    }
}
